package com.milkshopbackend.milkshop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.milkshopbackend.milkshop.Service.OrderService;
import com.milkshopbackend.milkshop.Service.ProductService;
import com.milkshopbackend.milkshop.Service.UserOperationLogService;
import com.milkshopbackend.milkshop.Service.UserService;
import com.milkshopbackend.milkshop.common.Result;
import com.milkshopbackend.milkshop.entity.Order;
import com.milkshopbackend.milkshop.entity.Product;
import com.milkshopbackend.milkshop.entity.User;
import com.milkshopbackend.milkshop.entity.UserOperationLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin")
@PreAuthorize("hasRole('ADMIN')")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserOperationLogService userOperationLogService;

    @GetMapping("/users")
    public Result<Page<User>> getUsers(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<User> userPage = new Page<>(page, size);
        return Result.success(userService.page(userPage));
    }

    @GetMapping("/orders")
    public Result<Page<Order>> getOrders(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Order> orderPage = new Page<>(page, size);
        return Result.success(orderService.page(orderPage));
    }

    @GetMapping("/products")
    public Result<Page<Product>> getProducts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Product> productPage = new Page<>(page, size);
        return Result.success(productService.page(productPage));
    }

    @GetMapping("/logs")
    public Result<Page<UserOperationLog>> getOperationLogs(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<UserOperationLog> logPage = new Page<>(page, size);
        LambdaQueryWrapper<UserOperationLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(UserOperationLog::getOperationTime);
        return Result.success(userOperationLogService.page(logPage, wrapper));
    }

    @PutMapping("/users/{userId}/status")
    public Result<Void> updateUserStatus(
            @PathVariable Long userId,
            @RequestParam Integer status) {
        User user = userService.getById(userId);
        if (user != null) {
            user.setStatus(status);
            userService.updateById(user);
            return Result.success();
        }
        return Result.error(404, "User not found");
    }

    @DeleteMapping("/products/{productId}")
    public Result<Void> deleteProduct(@PathVariable Long productId) {
        if (productService.removeById(productId)) {
            return Result.success();
        }
        return Result.error(404, "Product not found");
    }
} 